package com.think;

import com.think.bean.User;
import com.think.config.TestConfig;
import com.think.service.exp.BusinessExp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @desc:springboot启动类
 * @author:tudou
 * @date:2017/7/20 SpringBootApplication包含默认注解ComponentScanSpringBootConfiguration和EnableAutoConfiguration（会根据依赖的jar优化自己加载配置）。
 * 比如我们配置了spring-boot-starter-web，它是加载了tomcat和springmvc的
 * 此类放在所有class的平级或上级目录，否则会导致集成测试等问题
 */
@RestController
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.think.dao.mapper")
/*
ApplicationRunner, CommandLineRunner用于容器启动后的回调。
多个可以用@Order(1)排序。区别在于前者封装了参数。
* */
//SpringBootServletInitializer是使用war外部启动tomcat的入口
public class App extends SpringBootServletInitializer implements ApplicationRunner, CommandLineRunner {
    @Resource
    private JdbcTemplate jdbcTemplate;
    //@Autowired
    private TestConfig testConfig;

    public App() {
        super();
        //setRegisterErrorPageFilter(false);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }

    @RequestMapping("/")
    String index() {
        return "Hello World!";
    }

    @RequestMapping("/sql")
    String sql() {
        int result = jdbcTemplate.queryForObject("select count(*) from sso_user", null, Integer.class);
        return "Hello World!" + result;
    }

    @RequestMapping("/err")
    String error() {
        int a = 1 / 0;
        return "error";
    }

    @RequestMapping("/err2")
    String error2() {
        if (this.getClass().getName() != null) {
            throw new BusinessExp("errcode2", "custom error");
        }
        return "error2";
    }

    //只能通过war运行访问才可以看到error.jsp
    @RequestMapping("/err3")
    ModelAndView error3() {
        ModelAndView mv = new ModelAndView("error");
        return mv;
    }

    @RequestMapping("/json")
    User json() {
        User user = new User();
        user.setNick_name("tudou");
        user.setCreateTime(new Date());
        return user;
    }

    public static void main(String[] args) {
        //极简模式
        SpringApplication.run(App.class, args);
        /*SpringApplication application = new SpringApplication(App.class);
        Set<String> sources = new HashSet<>();//在1.x中为Set<Object>
        sources.add("classpath:applicationContext*.xml");
        application.setSources(sources);
        application.run(args);*/
    }

    @Override
    public void run(String... args) throws Exception {
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //System.out.println("sdk:" + testConfig.getSdk());
    }
}
